# (C) Datadog, Inc. 2018-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)

import os

# This is a small extract of metrics from varnish. This is meant to test that
# the check gather metrics. This the check return everything from varnish
# without any selection/rename, their is no point in having a complete list.
COMMON_METRICS = [
    "varnish.uptime",  # metrics where the "MAIN" prefix was removed
    "varnish.sess_conn",  # metrics where the "MAIN" prefix was removed
    "varnish.sess_fail",  # metrics where the "MAIN" prefix was removed
    "varnish.client_req_400",  # metrics where the "MAIN" prefix was removed
    "varnish.client_req_417",  # metrics where the "MAIN" prefix was removed
    "varnish.client_req",  # metrics where the "MAIN" prefix was removed
    "varnish.cache_hit",  # metrics where the "MAIN" prefix was removed
    "varnish.cache_hitpass",  # metrics where the "MAIN" prefix was removed
    "varnish.cache_miss",  # metrics where the "MAIN" prefix was removed
    "varnish.backend_conn",  # metrics where the "MAIN" prefix was removed
    "varnish.backend_unhealthy",  # metrics where the "MAIN" prefix was removed
    "varnish.backend_busy",  # metrics where the "MAIN" prefix was removed
    "varnish.fetch_eof",  # metrics where the "MAIN" prefix was removed
    "varnish.fetch_bad",  # metrics where the "MAIN" prefix was removed
    "varnish.fetch_none",  # metrics where the "MAIN" prefix was removed
    "varnish.fetch_1xx",  # metrics where the "MAIN" prefix was removed
    "varnish.pools",  # metrics where the "MAIN" prefix was removed
    "varnish.busy_sleep",  # metrics where the "MAIN" prefix was removed
    "varnish.busy_wakeup",  # metrics where the "MAIN" prefix was removed
    "varnish.busy_killed",  # metrics where the "MAIN" prefix was removed
    "varnish.sess_queued",  # metrics where the "MAIN" prefix was removed
    "varnish.sess_dropped",  # metrics where the "MAIN" prefix was removed
    "varnish.n_object",  # metrics where the "MAIN" prefix was removed
    "varnish.n_vampireobject",  # metrics where the "MAIN" prefix was removed
    "varnish.n_vcl",  # metrics where the "MAIN" prefix was removed
    "varnish.n_vcl_avail",  # metrics where the "MAIN" prefix was removed
    "varnish.n_vcl_discard",  # metrics where the "MAIN" prefix was removed
    "varnish.bans",  # metrics where the "MAIN" prefix was removed
    "varnish.bans_completed",  # metrics where the "MAIN" prefix was removed
    "varnish.bans_obj",  # metrics where the "MAIN" prefix was removed
    "varnish.bans_req",  # metrics where the "MAIN" prefix was removed
    "varnish.MGT.child_start",
    "varnish.MGT.child_exit",
    "varnish.MGT.child_stop",
    "varnish.MEMPOOL.busyobj.live",
    "varnish.MEMPOOL.busyobj.pool",
    "varnish.MEMPOOL.busyobj.allocs",
    "varnish.MEMPOOL.busyobj.frees",
    "varnish.SMA.s0.c_req",
    "varnish.SMA.s0.c_fail",
    "varnish.SMA.Transient.c_req",
    "varnish.SMA.Transient.c_fail",
    "varnish.VBE.boot.default.req",
    "varnish.LCK.backend.creat",
    "varnish.LCK.ban.creat",
    "varnish.LCK.ban.locks",
    "varnish.LCK.busyobj.creat",
    "varnish.LCK.mempool.creat",
    "varnish.LCK.vbe.creat",
    "varnish.LCK.vbe.destroy",
    "varnish.LCK.vcl.creat",
    "varnish.LCK.vcl.destroy",
    "varnish.LCK.vcl.locks",
    "varnish.n_purges",
    "varnish.LCK.backend.destroy",
    "varnish.LCK.backend.locks",
    "varnish.LCK.ban.destroy",
    "varnish.LCK.busyobj.destroy",
    "varnish.LCK.busyobj.locks",
    "varnish.LCK.cli.creat",
    "varnish.LCK.cli.destroy",
    "varnish.LCK.cli.locks",
    "varnish.LCK.exp.creat",
    "varnish.LCK.exp.destroy",
    "varnish.LCK.exp.locks",
    "varnish.LCK.hcb.creat",
    "varnish.LCK.hcb.destroy",
    "varnish.LCK.hcb.locks",
    "varnish.LCK.lru.creat",
    "varnish.LCK.lru.destroy",
    "varnish.LCK.lru.locks",
    "varnish.LCK.mempool.destroy",
    "varnish.LCK.mempool.locks",
    "varnish.LCK.objhdr.creat",
    "varnish.LCK.objhdr.destroy",
    "varnish.LCK.objhdr.locks",
    "varnish.LCK.pipestat.creat",
    "varnish.LCK.pipestat.destroy",
    "varnish.LCK.pipestat.locks",
    "varnish.LCK.sess.creat",
    "varnish.LCK.sess.destroy",
    "varnish.LCK.sess.locks",
    "varnish.LCK.sma.creat",
    "varnish.LCK.sma.destroy",
    "varnish.LCK.sma.locks",
    "varnish.LCK.vbe.locks",
    "varnish.LCK.vcapace.creat",
    "varnish.LCK.vcapace.destroy",
    "varnish.LCK.vcapace.locks",
    "varnish.LCK.vxid.creat",
    "varnish.LCK.vxid.destroy",
    "varnish.LCK.vxid.locks",
    "varnish.LCK.waiter.creat",
    "varnish.LCK.waiter.destroy",
    "varnish.LCK.waiter.locks",
    "varnish.LCK.wq.creat",
    "varnish.LCK.wq.destroy",
    "varnish.LCK.wq.locks",
    "varnish.LCK.wstat.creat",
    "varnish.LCK.wstat.destroy",
    "varnish.LCK.wstat.locks",
    "varnish.MEMPOOL.busyobj.randry",
    "varnish.MEMPOOL.busyobj.recycle",
    "varnish.MEMPOOL.busyobj.surplus",
    "varnish.MEMPOOL.busyobj.sz_actual",
    "varnish.MEMPOOL.busyobj.sz_wanted",
    "varnish.MEMPOOL.busyobj.timeout",
    "varnish.MEMPOOL.busyobj.toosmall",
    "varnish.MEMPOOL.req0.allocs",
    "varnish.MEMPOOL.req0.frees",
    "varnish.MEMPOOL.req0.live",
    "varnish.MEMPOOL.req0.pool",
    "varnish.MEMPOOL.req0.randry",
    "varnish.MEMPOOL.req0.recycle",
    "varnish.MEMPOOL.req0.surplus",
    "varnish.MEMPOOL.req0.sz_actual",
    "varnish.MEMPOOL.req0.sz_wanted",
    "varnish.MEMPOOL.req0.timeout",
    "varnish.MEMPOOL.req0.toosmall",
    "varnish.MEMPOOL.req1.allocs",
    "varnish.MEMPOOL.req1.frees",
    "varnish.MEMPOOL.req1.live",
    "varnish.MEMPOOL.req1.pool",
    "varnish.MEMPOOL.req1.randry",
    "varnish.MEMPOOL.req1.recycle",
    "varnish.MEMPOOL.req1.surplus",
    "varnish.MEMPOOL.req1.sz_actual",
    "varnish.MEMPOOL.req1.sz_wanted",
    "varnish.MEMPOOL.req1.timeout",
    "varnish.MEMPOOL.req1.toosmall",
    "varnish.MEMPOOL.sess0.allocs",
    "varnish.MEMPOOL.sess0.frees",
    "varnish.MEMPOOL.sess0.live",
    "varnish.MEMPOOL.sess0.pool",
    "varnish.MEMPOOL.sess0.randry",
    "varnish.MEMPOOL.sess0.recycle",
    "varnish.MEMPOOL.sess0.surplus",
    "varnish.MEMPOOL.sess0.sz_actual",
    "varnish.MEMPOOL.sess0.sz_wanted",
    "varnish.MEMPOOL.sess0.timeout",
    "varnish.MEMPOOL.sess0.toosmall",
    "varnish.MEMPOOL.sess1.allocs",
    "varnish.MEMPOOL.sess1.frees",
    "varnish.MEMPOOL.sess1.live",
    "varnish.MEMPOOL.sess1.pool",
    "varnish.MEMPOOL.sess1.randry",
    "varnish.MEMPOOL.sess1.recycle",
    "varnish.MEMPOOL.sess1.surplus",
    "varnish.MEMPOOL.sess1.sz_actual",
    "varnish.MEMPOOL.sess1.sz_wanted",
    "varnish.MEMPOOL.sess1.timeout",
    "varnish.MEMPOOL.sess1.toosmall",
    "varnish.MGT.child_died",
    "varnish.MGT.child_dump",
    "varnish.MGT.child_panic",
    "varnish.MGT.uptime",
    "varnish.SMA.Transient.c_bytes",
    "varnish.SMA.Transient.c_freed",
    "varnish.SMA.Transient.g_alloc",
    "varnish.SMA.Transient.g_bytes",
    "varnish.SMA.Transient.g_space",
    "varnish.SMA.s0.c_bytes",
    "varnish.SMA.s0.c_freed",
    "varnish.SMA.s0.g_alloc",
    "varnish.SMA.s0.g_bytes",
    "varnish.SMA.s0.g_space",
    "varnish.backend_fail",
    "varnish.backend_recycle",
    "varnish.backend_req",
    "varnish.backend_retry",
    "varnish.backend_reuse",
    "varnish.bans_added",
    "varnish.bans_deleted",
    "varnish.bans_dups",
    "varnish.bans_lurker_contention",
    "varnish.bans_lurker_obj_killed",
    "varnish.bans_lurker_obj_killed_cutoff",
    "varnish.bans_lurker_tested",
    "varnish.bans_lurker_tests_tested",
    "varnish.bans_obj_killed",
    "varnish.bans_persisted_bytes",
    "varnish.bans_persisted_fragmentation",
    "varnish.bans_tested",
    "varnish.bans_tests_tested",
    "varnish.cache_hitmiss",
    "varnish.esi_errors",
    "varnish.esi_warnings",
    "varnish.exp_mailed",
    "varnish.exp_received",
    "varnish.fetch_204",
    "varnish.fetch_304",
    "varnish.fetch_chunked",
    "varnish.fetch_failed",
    "varnish.fetch_head",
    "varnish.fetch_length",
    "varnish.fetch_no_thread",
    "varnish.hcb_insert",
    "varnish.hcb_lock",
    "varnish.hcb_nolock",
    "varnish.losthdr",
    "varnish.n_backend",
    "varnish.n_expired",
    "varnish.n_gunzip",
    "varnish.n_gzip",
    "varnish.n_lru_moved",
    "varnish.n_lru_nuked",
    "varnish.n_obj_purged",
    "varnish.n_objectcore",
    "varnish.n_objecthead",
    "varnish.n_test_gunzip",
    "varnish.req_dropped",
    "varnish.s_fetch",
    "varnish.s_pass",
    "varnish.s_pipe",
    "varnish.s_pipe_hdrbytes",
    "varnish.s_pipe_in",
    "varnish.s_pipe_out",
    "varnish.s_req_bodybytes",
    "varnish.s_req_hdrbytes",
    "varnish.s_resp_bodybytes",
    "varnish.s_resp_hdrbytes",
    "varnish.s_sess",
    "varnish.s_synth",
    "varnish.sc_overload",
    "varnish.sc_pipe_overflow",
    "varnish.sc_range_short",
    "varnish.sc_rem_close",
    "varnish.sc_req_close",
    "varnish.sc_req_http10",
    "varnish.sc_req_http20",
    "varnish.sc_resp_close",
    "varnish.sc_rx_bad",
    "varnish.sc_rx_body",
    "varnish.sc_rx_junk",
    "varnish.sc_rx_overflow",
    "varnish.sc_rx_timeout",
    "varnish.sc_tx_eof",
    "varnish.sc_tx_error",
    "varnish.sc_tx_pipe",
    "varnish.sc_vcl_failure",
    "varnish.sess_closed",
    "varnish.sess_closed_err",
    "varnish.sess_herd",
    "varnish.sess_readahead",
    "varnish.shm_cont",
    "varnish.shm_cycles",
    "varnish.shm_flushes",
    "varnish.shm_records",
    "varnish.shm_writes",
    "varnish.summs",
    "varnish.thread_queue_len",
    "varnish.threads",
    "varnish.threads_created",
    "varnish.threads_destroyed",
    "varnish.threads_failed",
    "varnish.threads_limited",
    "varnish.vcl_fail",
    "varnish.vmods",
    "varnish.VBE.boot.default.bereq_bodybytes",
    "varnish.VBE.boot.default.bereq_hdrbytes",
    "varnish.VBE.boot.default.beresp_bodybytes",
    "varnish.VBE.boot.default.beresp_hdrbytes",
    "varnish.VBE.boot.default.conn",
    "varnish.VBE.boot.default.pipe_hdrbytes",
    "varnish.VBE.boot.default.pipe_in",
    "varnish.VBE.boot.default.pipe_out",
]
METRICS_5 = [
    "varnish.sess_drop",  # metrics where the "MAIN" prefix was removed
    "varnish.LCK.backend_tcp.creat",
    "varnish.LCK.backend_tcp.destroy",
    "varnish.LCK.backend_tcp.locks",
    # This is a rate that is artificially emitted with the same value as varnish.n_purges
    # which is a gauge in versions <=5
    "varnish.n_purgesps",
]
METRICS_6 = [
    "varnish.LCK.backend.dbg_busy",
    "varnish.LCK.tcp_pool.creat",
    "varnish.LCK.tcp_pool.destroy",
    "varnish.LCK.tcp_pool.locks",
    "varnish.LCK.ban.dbg_busy",
    "varnish.LCK.ban.dbg_try_fail",
    "varnish.LCK.backend.dbg_try_fail",
    "varnish.LCK.busyobj.dbg_busy",
    "varnish.LCK.busyobj.dbg_try_fail",
    "varnish.LCK.cli.dbg_busy",
    "varnish.LCK.cli.dbg_try_fail",
    "varnish.LCK.exp.dbg_busy",
    "varnish.LCK.exp.dbg_try_fail",
    "varnish.LCK.hcb.dbg_busy",
    "varnish.LCK.hcb.dbg_try_fail",
    "varnish.LCK.lru.dbg_busy",
    "varnish.LCK.lru.dbg_try_fail",
    "varnish.LCK.mempool.dbg_busy",
    "varnish.LCK.mempool.dbg_try_fail",
    "varnish.LCK.objhdr.dbg_busy",
    "varnish.LCK.objhdr.dbg_try_fail",
    "varnish.LCK.perpool.destroy",
    "varnish.LCK.perpool.locks",
    "varnish.LCK.perpool.creat",
    "varnish.LCK.perpool.dbg_busy",
    "varnish.LCK.perpool.dbg_try_fail",
    "varnish.LCK.pipestat.dbg_busy",
    "varnish.LCK.pipestat.dbg_try_fail",
    "varnish.LCK.probe.creat",
    "varnish.LCK.probe.destroy",
    "varnish.LCK.probe.locks",
    "varnish.LCK.probe.dbg_busy",
    "varnish.LCK.probe.dbg_try_fail",
    "varnish.LCK.sess.dbg_busy",
    "varnish.LCK.sess.dbg_try_fail",
    "varnish.LCK.sma.dbg_busy",
    "varnish.LCK.sma.dbg_try_fail",
    "varnish.LCK.tcp_pool.dbg_busy",
    "varnish.LCK.tcp_pool.dbg_try_fail",
    "varnish.LCK.vcapace.dbg_busy",
    "varnish.LCK.vcapace.dbg_try_fail",
    "varnish.LCK.vbe.dbg_busy",
    "varnish.LCK.vbe.dbg_try_fail",
    "varnish.LCK.vcl.dbg_busy",
    "varnish.LCK.vcl.dbg_try_fail",
    "varnish.LCK.vxid.dbg_busy",
    "varnish.LCK.vxid.dbg_try_fail",
    "varnish.LCK.waiter.dbg_busy",
    "varnish.LCK.waiter.dbg_try_fail",
    "varnish.LCK.wq.dbg_busy",
    "varnish.LCK.wq.dbg_try_fail",
    "varnish.LCK.wstat.dbg_busy",
    "varnish.LCK.wstat.dbg_try_fail",
    "varnish.VBE.boot.default.busy",
    "varnish.VBE.boot.default.fail",
    "varnish.VBE.boot.default.fail_eacces",
    "varnish.VBE.boot.default.fail_eaddrnotavail",
    "varnish.VBE.boot.default.fail_econnrefused",
    "varnish.VBE.boot.default.fail_enetunreach",
    "varnish.VBE.boot.default.fail_etimedout",
    "varnish.VBE.boot.default.fail_other",
    "varnish.VBE.boot.default.helddown",
    "varnish.VBE.boot.default.unhealthy",
    "varnish.beresp_shortlived",
    "varnish.beresp_uncacheable",
    "varnish.cache_hit_grace",
    "varnish.client_resp_500",
    "varnish.n_lru_limited",
    "varnish.n_pipe",
    "varnish.pipe_limited",
    "varnish.sc_rx_close_idle",
    "varnish.sess_fail_ebadf",
    "varnish.sess_fail_econnaborted",
    "varnish.sess_fail_eintr",
    "varnish.sess_fail_emfile",
    "varnish.sess_fail_enomem",
    "varnish.sess_fail_other",
    "varnish.ws_backend_overflow",
    "varnish.ws_client_overflow",
    "varnish.ws_session_overflow",
    "varnish.ws_thread_overflow",
]

VARNISHADM_PATH = "varnishadm"
SECRETFILE_PATH = "secretfile"
DAEMON_ADDRESS = "localhost:6082"

HERE = os.path.join(os.path.dirname(__file__))
FIXTURE_DIR = os.path.join(HERE, "fixtures")

CHECK_NAME = "varnish"

VARNISH_VERSION = os.getenv('VARNISH_VERSION')


def get_config_by_version(name=None):
    config = {"varnishstat": get_varnish_stat_path(), "tags": ["varnish_cluster:webs"]}

    if name:
        config["name"] = name
    return config


def get_varnish_stat_path():
    return "docker exec ci_varnish varnishstat"
